package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.Employee;
import com.itheima.reggie.service.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.time.LocalDateTime;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/employee")
public class EmployeeController {
    @Autowired
    private EmployeeService employeeService;

    @PostMapping("/login")
    public R<Employee> login(HttpServletRequest request, @RequestBody Employee employee){
        //1.将页面提交的password密码进行md5加密处理
        String password = employee.getPassword();
        password=DigestUtils.md5DigestAsHex(password.getBytes());


        LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(employee.getUsername()!=null,Employee::getUsername,employee.getUsername());
        List<Employee> list = employeeService.list(queryWrapper);
        if (list==null){
            return R.error("用户名错误");
        }
        for (Employee employee1 : list) {
            if (employee1.getPassword().equals(password)){
                if (employee1.getStatus()==0){
                    return R.error("该账户已被禁用");
                }
                request.getSession().setAttribute("employee",employee1.getId());
                return R.success(employee1);
            }
        }
        return R.error("密码错误");
    }


    /**
     * 账号退出
     * @param request
     * @return
     */
    @PostMapping("/logout")
    public R<String> logout(HttpServletRequest request){
        request.removeAttribute("employee");
        return R.success("退出成功！");
    }

    /**
     * 查询页面
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/page")
    public R<Page> page(int page ,int pageSize,String name){
        Page iPage=new Page(page,pageSize);
        LambdaQueryWrapper<Employee> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.hasText(name),Employee::getName,name);
        queryWrapper.orderByDesc(Employee::getUpdateTime);
        employeeService.page(iPage,queryWrapper);
        return R.success(iPage);
    }

    /**
     * 添加
     * @param employee
     * @return
     */
    @PostMapping
    public R<String> sa(HttpServletRequest request, @RequestBody Employee employee){
        log.info(employee.toString());
        employee.setUpdateTime(LocalDateTime.now());
        employee.setCreateTime(LocalDateTime.now());
        String password="123456";
        password=DigestUtils.md5DigestAsHex(password.getBytes());
        employee.setPassword(password);
        employee.setStatus(1);
        Long empId = (Long) request.getSession().getAttribute("employee");
        employee.setCreateUser(empId);
        employee.setUpdateUser(empId);
        employeeService.save(employee);
        return R.success("添加成功！");
    }

    /**
     * 根据员工id修改启用禁用状态
     * @param employee
     * @return
     */
    @PutMapping
    public R<String> update(HttpServletRequest request,@RequestBody Employee employee){
        log.info(employee.toString());
        Long empId = (Long) request.getSession().getAttribute("employee");
        employee.setUpdateTime(LocalDateTime.now());//更新修改时间
        employee.setUpdateUser(empId);//更新修改id
        employeeService.updateById(employee);
        return R.success("员工信息修改成功");
    }

    @GetMapping("{id}")
    public R<Employee> s(@PathVariable Long id){
        log.info(id.toString());
        Employee byId = employeeService.getById(id);
        return R.success(byId);
    }

}
